package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.pojo.entity.SystemAdminDept;
import com.lwc.cfdns.pojo.entity.SystemAdminRole;
import com.lwc.cfdns.service.SystemAdminRoleService;
import com.lwc.cfdns.mapper.SystemAdminRoleMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spring
 * @description 针对表【cfdns_system_admin_role(用户和角色关联表)】的数据库操作Service实现
 * @createDate 2023-05-24 20:52:46
 */
@Service
public class SystemAdminRoleServiceImpl extends ServiceImpl<SystemAdminRoleMapper, SystemAdminRole>
        implements SystemAdminRoleService {

    /**
     * 管理员角色
     *
     * @param admin_id
     * @param role_ids
     * @return boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 20:55:04
     */
    @Override
    public boolean roles(Long admin_id, List<Long> role_ids) {
        this.baseMapper.delete(new LambdaQueryWrapper<SystemAdminRole>().eq(SystemAdminRole::getAdminId, admin_id));
        if (role_ids.size() > 0) {
            List<SystemAdminRole> batInsert = new ArrayList<>();
            for (Long role_id : role_ids) {
                SystemAdminRole systemAdminRole = new SystemAdminRole();
                systemAdminRole.setAdminId(admin_id);
                systemAdminRole.setRoleId(role_id);
                batInsert.add(systemAdminRole);
            }
            return this.saveBatch(batInsert);
        }
        return false;
    }


    /**
     * 查询角色下的用户
     *
     * @param roleId
     * @return List<Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 21:44
     */
    @Override
    public List<Object> getAdminIds(Long roleId) {
        List<Object> admin_ids = this.baseMapper.selectObjs(new LambdaQueryWrapper<SystemAdminRole>().eq(SystemAdminRole::getRoleId, roleId).select(SystemAdminRole::getAdminId));
        return admin_ids;
    }

    /**
     * 批量给角色添加管理员授权
     *
     * @param roleId
     * @param adminIds
     * @return boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 14:00:57
     */
    @Override
    public boolean batAddAdminIdsOfRoles(Long roleId, List<Long> adminIds) {
        if (adminIds.size() > 0) {
            //先批理移除
            this.remove(new LambdaQueryWrapper<SystemAdminRole>().eq(SystemAdminRole::getRoleId, roleId).in(SystemAdminRole::getAdminId, adminIds));
            List<SystemAdminRole> batInsert = new ArrayList<>();
            for (Long admin_id : adminIds) {
                SystemAdminRole systemAdminRole = new SystemAdminRole();
                systemAdminRole.setAdminId(admin_id);
                systemAdminRole.setRoleId(roleId);
                batInsert.add(systemAdminRole);
            }
            return this.saveBatch(batInsert);
        }
        return false;
    }

    /**
     * 批量给角色取消管理员授权
     *
     * @param roleId
     * @param adminIds
     * @return boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 14:00:57
     */
    @Override
    public boolean batCancelAdminIdsOfRoles(Long roleId, List<Long> adminIds) {
        if (adminIds.size() > 0) {
            return this.remove(new LambdaQueryWrapper<SystemAdminRole>().eq(SystemAdminRole::getRoleId, roleId).in(SystemAdminRole::getAdminId, adminIds));
        }
        return false;
    }
}




